package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AjaxServlet
 */
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String flag = request.getParameter("flag");
		
		if("demo1".equals(flag)) {
			demo1(request,response);
			
		}
		else if("demo2".equals(flag)) {
			demo2(request,response);
		}
	}

	private void demo1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(request.getParameter("userName"));
		System.out.println(request.getParameter("password"));
		System.out.println(request.getParameter("age"));
		
		request.setAttribute("msg", "yes");
		request.getRequestDispatcher("test.jsp").forward(request, response);
	}

	private void demo2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		System.out.println(request.getParameter("userName"));
		System.out.println(request.getParameter("password"));
		System.out.println(request.getParameter("age"));
		
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print("ajax请求处理成功");
	
	}

}
